package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.utility.CFiError;
import com.checkfree.isolutions.property.PropertiesConfig;

import java.awt.Color;
import java.io.FileOutputStream;
import java.io.*;
import java.util.*;
import java.text.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.*;
import javax.servlet.http.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Chunk;
import com.lowagie.text.Font;
import com.lowagie.text.Phrase;
import com.lowagie.text.Element;
import com.lowagie.text.Image;

import com.checkfree.isolutions.customer.persist.*;
import com.checkfree.isolutions.statement.*;
import com.checkfree.isolutions.statement.persist.*;
import com.checkfree.isolutions.property.Properties;
import com.checkfree.isolutions.property.PropertiesConfig;



public class PDFServlet_v2r00 extends EbppxTransactionServlet {

    public void doTransaction(HttpServletRequest req, HttpServletResponse response)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);
            Merchant cfiMerchant = MerchantHomeImpl.singleton().findByName(ecsUser.getMerchantName());
            User cfiUser = getUserHome().findByLogonIdAndMerchant(ecsUser.getUserId(),cfiMerchant);

            final String encryptedStmtIndex = req.getParameter("stmtIndex");
            String stmtIndex = "";

            if(encryptedStmtIndex==null){
				stmtIndex = "0";
			}else{
				stmtIndex = EncryptionHelper.decrypt(encryptedStmtIndex,randomKey);
			}

            if (stmtIndex.length() == 0) {
                stmtIndex = "0";
            }
            final int stmtIdx =  Integer.parseInt(stmtIndex);

            final String sOidAccount = req.getParameter(EbppxConstants.ACCOUNT_ID);

            if (sOidAccount.length() == 0) {
                throw new ServletException("Missing account identifier");
            }

            final String type = req.getParameter("type");
            int startMonth = EbppxCusConstants.STMT_START_MONTH;

            if(type!=null && type.equals("csr")){
				startMonth = -11;
			}

			//get information from checkfree database
            final Account cfiAccount = getAccountHome().findById(new ObjectId(EncryptionHelper.decrypt(sOidAccount,randomKey)));
            final Vector cfiStatements = StatementUtil.getStatements(cfiAccount,startMonth);
            final Statement cfiStatement = StatementUtil.getStatement(cfiAccount,startMonth,stmtIdx);

            String stmtIndexPrev = "-1";
            if ((stmtIdx+1) < cfiStatements.size()) {
                stmtIndexPrev = String.valueOf(stmtIdx+1);
            }


            //final String download1stPageUrl = req.getParameter("download1stPageUrl");
			String download1stPageUrl = EbppxCusConstants.DLOAD_PDF_SINGLE;
			Date dtStmtDate = cfiStatement.getDate();
			String stmtDate = DateUtil.formatDate(dtStmtDate,"dd/mm/yyyy");
			final long instanceId = cfiStatement.getInstanceId();
			com.checkfree.isolutions.admin.ObjectId objectId = new com.checkfree.isolutions.admin.ObjectId(com.checkfree.isolutions.streaming.IClassId.STATEMENT, (int)instanceId);
			String statementId = objectId.toString(true);

			String param = "?stmtIndex=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(Integer.toString(stmtIdx),randomKey)) + "&statementId=" + EncryptionHelper.encodeURL(EncryptionHelper.encrypt(statementId,randomKey)) + "&accountId=" + EncryptionHelper.encodeURL(sOidAccount);
			download1stPageUrl = download1stPageUrl + EbppxUtil.getVersion(download1stPageUrl,stmtDate) + param;

            final String update = req.getParameter("update");
            String acctNo = cfiAccount.getExternalKey();

            if(update!=null && update.equals("yes")){
				cfiUser.setProperty("CorporateBillTNC","yes");
			}

            String corpBillTNC = cfiUser.getProperty("CorporateBillTNC");
            boolean boolCorpCust = false;

            if(AcctDetails.getCorpCust(acctNo).equals("1") || AcctDetails.getCorpCust(acctNo).equals("2") || AcctDetails.getCorpCust(acctNo).equals("3") || AcctDetails.getCorpCust(acctNo).equals("4")){
				boolCorpCust = true;
			}

			if(boolCorpCust &&!corpBillTNC.equals("")){

           	//Get section from root section
           	final StatementSection cfiRootSection = cfiStatement.getRootSection();
		   	final StatementSection cfiSummarySection = cfiRootSection.getSection("SummarySection");
		   	final StatementSection cfiBillingPeriodSection = cfiRootSection.getSection("BillingPeriodSection");
			final StatementSection cfiDepositPaidSection = cfiRootSection.getSection("DepositPaidSection");
			final StatementSection cfiCreditLimitSection = cfiRootSection.getSection("CreditLimitSection");
			final StatementSection cfiImportantMessagesSection = cfiRootSection.getSection("ImportantMessagesSection");
			final StatementSection cfiCurrentPromotionSection = cfiRootSection.getSection("CurrentPromotionSection");

			//Get section from summary section
		   	final StatementSection cfiRegistrationChargesSection = cfiSummarySection.getSection("RegistrationChargesSection");
		   	final StatementSection cfiMonthlyChargesSection = cfiSummarySection.getSection("MonthlyChargesSection");
		   	final StatementSection cfiValueAddedServicesSection = cfiSummarySection.getSection("ValueAddedServicesSection");
		   	final StatementSection cfiUsageChargesSection = cfiSummarySection.getSection("UsageChargesSection");
		   	final StatementSection cfiServiceTaxSection = cfiSummarySection.getSection("ServiceTaxSection");
		   	final StatementSection cfiOtherChargesSection = cfiSummarySection.getSection("OtherChargesSection");
		   	final StatementSection cfiDiscountSection = cfiSummarySection.getSection("DiscountSection");

			//Get data field from root section
			final String AccNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("AccountNumber"));
			final String StmtDt = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("StatementDate"));
			final String AccName = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("AccountName"));
		  	final String TotalAmountDue = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("TotalAmountDue"));

			// Added by Meau for rounding
		  	String TotalAmount = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("TotalAmount"));

			// end added by Meau for rounding

		  	final String PhoneNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("PhoneNumber"));
		  	final String RefNo = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("ReferenceNumber"));
		  	final String CurrentChargesDueDate = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("CurrentChargesDueDate"));
		  	final String Address1 = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("Address1"));
		  	final String Address2 = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("Address2"));
		  	final String Address3 = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("Address3"));
		  	final String Address4 = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("Address4"));
		  	final String MarketSegment = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("MarketSegment"));
		  	final String OldAccountNumber = EbppxUtil.htmlEncode(cfiRootSection.getFieldText("OldAccountNumber"));

		  	//Get data field from billing period section
		  	String BillPeriod = "";
		  	if(cfiBillingPeriodSection!=null){
				BillPeriod = EbppxUtil.htmlEncode(cfiBillingPeriodSection.getFieldText("BillingPeriod"));
			}

		  	//Get data field from deposit paid section
		  	String DepositPaid = "";
		  	if(cfiDepositPaidSection!=null){
				DepositPaid = EbppxUtil.htmlEncode(cfiDepositPaidSection.getFieldText("DepositPaid"));
			}

		  	//Get data field from credit limit section
		  	String CreditLimit = "";
		  	if(cfiCreditLimitSection!=null){
				CreditLimit = EbppxUtil.htmlEncode(cfiCreditLimitSection.getFieldText("CreditLimit"));
			}

		  	//Get data field from Summary section
		  	final String PreviousBalance = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("PreviousBalance"));
		  	final String PaymentReceived = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("PaymentReceived"));
		  	final String Adjustment = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("Adjustment"));
		  	final String BalanceForward = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("BalanceForward"));
		  	final String TotalCurrentCharges = EbppxUtil.htmlEncode(cfiSummarySection.getFieldText("TotalCurrentCharges"));

		  	//Get data field from registration charges section
		  	String RegistrationCharges = "";
		  	if(cfiRegistrationChargesSection!=null){
				RegistrationCharges = EbppxUtil.htmlEncode(cfiRegistrationChargesSection.getFieldText("Amount"));
			}

		  	//Get data field from usage charges section
		  	String UsageCharges = "";
		  	if(cfiUsageChargesSection!=null){
				UsageCharges = EbppxUtil.htmlEncode(cfiUsageChargesSection.getFieldText("Amount"));
			}

		  	//Get data field from monthly charges section
		  	String MonthlyCharges = "";
		  	if(cfiMonthlyChargesSection!=null){
				MonthlyCharges = EbppxUtil.htmlEncode(cfiMonthlyChargesSection.getFieldText("Amount"));
			}

			//Get data field from service tax section
			String ServiceTax = "";
			if(cfiServiceTaxSection!=null){
				ServiceTax = EbppxUtil.htmlEncode(cfiServiceTaxSection.getFieldText("Amount"));
			}

			//Get data field from value added services section
			String ValueAddedServices = "";
			if(cfiValueAddedServicesSection!=null){
				ValueAddedServices = EbppxUtil.htmlEncode(cfiValueAddedServicesSection.getFieldText("Amount"));
			}

			//Get data field from other charges section
			String otherCharges = "";
			if(cfiOtherChargesSection!=null){
				otherCharges = EbppxUtil.htmlEncode(cfiOtherChargesSection.getFieldText("Amount"));
			}

			//Get data field from discount section
			String discount = "";
			if(cfiDiscountSection!=null){
				discount = EbppxUtil.htmlEncode(cfiDiscountSection.getFieldText("Amount"));
			}

			String MessageLine1 = "";
			String MessageLine2 = "";
			String MessageLine4 = "";
			String MessageLine5 = "";
			String MessageLine6 = "";
			String MessageLine8 = "";
			String MessageLine10 = "";
			String MessageLine12 = "";
			String MessageLine13 = "";
			String MessageLine14 = "";
			String MessageLine16 = "";
			String MessageLine17 = "";
			String MessageLine18 = "";
			if(cfiImportantMessagesSection!=null){
				//Get data field from important messages section
				MessageLine1 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine1"));
				MessageLine2 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine2"));
				MessageLine4 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine4"));
				MessageLine5 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine5"));
				MessageLine6 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine6"));
				MessageLine8 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine8"));
				MessageLine10 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine10"));
				MessageLine12 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine12"));
				MessageLine13 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine13"));
				MessageLine14 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine14"));
				MessageLine16 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine16"));
				MessageLine17 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine17"));
				MessageLine18 = EbppxUtil.htmlEncode(cfiImportantMessagesSection.getFieldText("MessageLine18"));
			}

			//get message details from config file
			//com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

			//String MessageEnglish = appCfg.getProperty("MessageEnglish");
			//String MessageMalay = appCfg.getProperty("MessageMalay");

			//Padding Amount
			int tpPadBottom = 8;
			int tpPadTop = 3;
			int tpPadRight = 8;
			int tpPadLeft = 8;

			final Date dtVal = cfiStatement.getDate();
            final DateFormat dfdisp = new SimpleDateFormat("ddMMMyyyy");
            String stdate = EbppxUtil.htmlEncode(dfdisp.format(dtVal));

            String filename = new String(cfiAccount.getExternalKey()+"-"+stdate);
           	filename += ".pdf";

		   	response.setContentType("application/pdf");
		   	 //response.setHeader("Content-Disposition","attachment; filename=" + filename);

		   		Document document = new Document();

		   		PdfWriter.getInstance(document,response.getOutputStream());
		   		document.open();

				//Write data to PDF

                //Crate Main Table for page position purpose
                float[] tableColsWidth = {4f, 2f};
                PdfPTable table = new PdfPTable(tableColsWidth);

                //set the table width percentage of the page
                table.setWidthPercentage(100);

				//Declare
				Chunk chunk;
				Phrase phrase;
				Paragraph paragraph;
				PdfPCell cell;
				Font font = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL,new Color(0, 0, 0));
				Font fontItalic = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.ITALIC,new Color(0, 0, 0));
				Font fontBold = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.BOLD,new Color(0, 0, 0));
				Font fontSpace = FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL,new Color(0, 0, 0));
				Font bigFontItalic = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.ITALIC,new Color(0, 0, 0));
				Font bigFontBold = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.BOLD,new Color(0, 0, 0));

				//Title
				phrase = new Phrase();
				chunk = new Chunk("STATEMENT OF ACCOUNT\n", bigFontBold);
				phrase.add(chunk);
				chunk = new Chunk("PENYATA AKAUN", bigFontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setVerticalAlignment(Element.ALIGN_BOTTOM);
				cell.setColspan(2);
				cell.setBorderWidth(0);
				table.addCell(cell);

				//Account Name
				phrase = new Phrase();
				chunk = new Chunk("Account Name / ", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Nama Akaun : ", fontItalic);
				phrase.add(chunk);
				chunk = new Chunk(AccName, fontBold);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(2);
				cell.setBorderWidth(0);
				table.addCell(cell);

				//Add blank row
				cell = new PdfPCell(new Phrase("",fontBold));
				cell.setColspan(2);
				cell.setBorderWidth(0);
				table.addCell(cell);

				//Crate sub Table inside main table
                float[] subTableColsWidth = {3f, 4f, 2f, 3f};
                PdfPTable subTable = new PdfPTable(subTableColsWidth);

				//sub table header1
				phrase = new Phrase();
				chunk = new Chunk("Previous Balance\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Baki Dahulu ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				subTable.addCell(cell);

				//sub table header2
				phrase = new Phrase();
				chunk = new Chunk("Payment Received\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Jumlah Bayaran Diterima ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				subTable.addCell(cell);

				//sub table header3
				phrase = new Phrase();
				chunk = new Chunk("Adjustments\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Penyelarasan ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				subTable.addCell(cell);

				//sub table header4
				phrase = new Phrase();
				chunk = new Chunk("Balance Forward\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Baki Ke Hadapan ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				subTable.addCell(cell);

				//sub table Previous Balance
				phrase = new Phrase();
				chunk = new Chunk(PreviousBalance, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//sub table Payment Received
				phrase = new Phrase();
				chunk = new Chunk(PaymentReceived, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//sub table Adjustment
				phrase = new Phrase();
				chunk = new Chunk(Adjustment, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//sub table Balance Forward
				phrase = new Phrase();
				chunk = new Chunk(BalanceForward, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//add big blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(4);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//Current charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Current Charges\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Caj Semasa", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//Current charges Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(TotalCurrentCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//add blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(4);
				subTable.addCell(cell);

				//Current charges due date Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Current Charges Due Date\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Tarikh Akhir Bayaran Caj Semasa", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//Current charges due date Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(CurrentChargesDueDate, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//add blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(4);
				subTable.addCell(cell);
/////////////////// Added by Meau for rounding/////////////////////////////////////////
				if(TotalAmount!=null && !TotalAmount.equals("")){
					//Total amount Title at sub table
					phrase = new Phrase();
					chunk = new Chunk("Total amount \n", fontBold);
					phrase.add(chunk);
					chunk = new Chunk("Jumlah", fontItalic);
					phrase.add(chunk);
                	cell = new PdfPCell(phrase);
                	cell.setColspan(3);
                	cell.setBorderWidth(0);
					cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
					subTable.addCell(cell);

					//Total amount due Content at sub table
					phrase = new Phrase();
					chunk = new Chunk(TotalAmount, font);
					phrase.add(chunk);
                	cell = new PdfPCell(phrase);
					cell.setHorizontalAlignment(Element.ALIGN_CENTER);
					cell.setPaddingTop(tpPadTop);
					cell.setPaddingBottom(tpPadBottom);
					subTable.addCell(cell);

					//add blank row at sub table
					cell = new PdfPCell(new Phrase("",fontSpace));
					cell.setBorderWidth(0);
					cell.setColspan(4);
					cell.setPaddingTop(tpPadTop);
					cell.setPaddingBottom(tpPadBottom);
					subTable.addCell(cell);
				}

//////////////// Added by Meau for image ////////////////////////////
								//Get Image Detail from config
								com.checkfree.isolutions.property.Properties appCfg = com.checkfree.isolutions.property.PropertiesConfig.getApplicationSettings();

								if (appCfg.hasProperty("PdfPrePrintedForm") == false){
									throw new IllegalArgumentException("Image Path not specified!");
								}else{
									String ImagePath = appCfg.getProperty("PdfPrePrintedForm");

									//Add Maxis Image Background
									Image image = Image.getInstance(ImagePath);
									image.setAbsolutePosition(-40, 0);
									image.scalePercent(85,85);
									image.setAlignment(Image.UNDERLYING);
									document.add(image);
								}

////////////////// end added by Meau for image/////////////////////////////
////////////////// end added by Meau/////////////////////////////

				//Total amount due Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Total amount due\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Jumlah Perlu Dibayar", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//Total amount due Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(TotalAmountDue, fontBold);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//add blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(4);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//Account Summary Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Account Summary\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Ringkasan Akaun", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorder(PdfPCell.RIGHT + PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				subTable.addCell(cell);

				//Amount Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Amount (RM)\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Amaun (RM)", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setPaddingLeft(tpPadLeft);
                cell.setBorder(PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				subTable.addCell(cell);

				//Balance Forward Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Balance Forward\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Baki Ke Hadapan", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorder(PdfPCell.RIGHT + PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				subTable.addCell(cell);

				//Balance Forward Amount at sub table
				phrase = new Phrase();
				chunk = new Chunk(BalanceForward, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setPaddingRight(tpPadRight);
                cell.setBorder(PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//Registration Charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Registration Charges", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Registration Charges Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(RegistrationCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Monthly Charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Monthly Charges", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Registration Charges Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(MonthlyCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Value Added Services Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Value Added Services", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Value Added Services Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(ValueAddedServices, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Usage Charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Usage Charges", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Usage Charges Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(UsageCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Other Charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Other Charges", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Other Charges Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(otherCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Discount & Rebates Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Discount & Rebates", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Discount & Rebates Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(discount, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Service Tax Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Service Tax (5%)", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(2);
                cell.setBorderWidth(0);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Service Tax Content at sub table
				phrase = new Phrase();
				chunk = new Chunk(ServiceTax, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setBorder(PdfPCell.RIGHT);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setPaddingRight(tpPadRight);
				subTable.addCell(cell);

				//Blank Column for Amount Section at sub table
                cell = new PdfPCell(new Phrase("",font));
                cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Current Charges Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Current Charges\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Caj Semasa", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorder(PdfPCell.RIGHT + PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				subTable.addCell(cell);

				//Current Charges Amount at sub table
				phrase = new Phrase();
				chunk = new Chunk(TotalCurrentCharges, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setPaddingRight(tpPadRight);
                cell.setBorder(PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//Total Amount Due Title at sub table
				phrase = new Phrase();
				chunk = new Chunk("Total Amount Due\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Jumlah Perlu Dibayar", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setColspan(3);
                cell.setBorder(PdfPCell.RIGHT + PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				subTable.addCell(cell);

				//Total Amount Due Amount at sub table
				phrase = new Phrase();
				chunk = new Chunk(TotalAmountDue, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
                cell.setPaddingRight(tpPadRight);
                cell.setBorder(PdfPCell.BOTTOM);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				subTable.addCell(cell);

				//add blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorder(PdfPCell.BOTTOM);
				cell.setColspan(4);
				subTable.addCell(cell);

				//add big blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(4);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//Bill message
				phrase = new Phrase();
				chunk = new Chunk("Bill message / ", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Maklumat Bil:", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Bill message row 1
				phrase = new Phrase();
				chunk = new Chunk(formatStr(MessageLine1), font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.TOP + PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 2
				phrase = new Phrase();
				chunk = new Chunk(MessageLine2, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 4
				phrase = new Phrase();
				chunk = new Chunk(MessageLine4, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 5
				phrase = new Phrase();
				chunk = new Chunk(MessageLine5, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 6
				phrase = new Phrase();
				chunk = new Chunk(MessageLine6, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 8
				phrase = new Phrase();
				chunk = new Chunk(MessageLine8 + "\n\n", font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);				;

				//Bill message row 10
				phrase = new Phrase();
				chunk = new Chunk(formatStr(MessageLine10), fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 12
				phrase = new Phrase();
				chunk = new Chunk(MessageLine12, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 13
				phrase = new Phrase();
				chunk = new Chunk(MessageLine13, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 14
				phrase = new Phrase();
				chunk = new Chunk(MessageLine14, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 16
				phrase = new Phrase();
				chunk = new Chunk(MessageLine16, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 17
				phrase = new Phrase();
				chunk = new Chunk(MessageLine17, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT);
				subTable.addCell(cell);

				//Bill message row 18
				phrase = new Phrase();
				chunk = new Chunk(MessageLine18, fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(4);
				cell.setPaddingLeft(tpPadLeft);
				cell.setPaddingRight(tpPadRight);
				cell.setPaddingBottom(tpPadBottom);
				cell.setBorder(PdfPCell.LEFT + PdfPCell.RIGHT + PdfPCell.BOTTOM);
				subTable.addCell(cell);

				//Add subTable
				cell = new PdfPCell(subTable);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorder(PdfPCell.RIGHT + PdfPCell.BOTTOM);
				cell.setPaddingRight(tpPadRight);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				table.addCell(cell);


				//Reuse sub Table inside main table second column
                float[] subTableColsWidth2 = {1f, 1f};
                subTable = new PdfPTable(subTableColsWidth2);

				//Account Number Title
				phrase = new Phrase();
				chunk = new Chunk("Account No.\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("No.Akaun", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Account Number content
				phrase = new Phrase();
				chunk = new Chunk(": " + AccNo + " (new) / " + OldAccountNumber, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Reference No. Title
				phrase = new Phrase();
				chunk = new Chunk("Reference No.\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("No.Rujukan", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Reference No. content
				phrase = new Phrase();
				chunk = new Chunk(": " + RefNo, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Statement Date Title
				phrase = new Phrase();
				chunk = new Chunk("Statement Date\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("Tarikh Penyata", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Statement Date content
				phrase = new Phrase();
				chunk = new Chunk(": " + StmtDt, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Billing Period Title
				phrase = new Phrase();
				chunk = new Chunk("Billing Period\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("Tempoh Bil", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Billing Period content
				phrase = new Phrase();
				chunk = new Chunk(": " + BillPeriod, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Deposit Paid Title
				phrase = new Phrase();
				chunk = new Chunk("Deposit Paid.\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("Cagaran", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Deposit Paid content
				phrase = new Phrase();
				chunk = new Chunk(": " + DepositPaid, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Credit Limit Title
				phrase = new Phrase();
				chunk = new Chunk("Credit Limit\n ", font);
				phrase.add(chunk);
				chunk = new Chunk("Had Kredit", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//Credit Limit content
				phrase = new Phrase();
				chunk = new Chunk(": " + CreditLimit, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Add blank row
				cell = new PdfPCell(new Phrase("",fontBold));
				cell.setColspan(2);
				cell.setBorder(PdfPCell.BOTTOM);
				subTable.addCell(cell);

				//Current Promotion title
				phrase = new Phrase();
				chunk = new Chunk("Current Promotion / ", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Promosi Terkini:", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(2);
				cell.setBorderWidth(0);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingLeft(tpPadLeft);
				subTable.addCell(cell);

				//add big blank row at sub table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(2);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//Current Promotion repeat section
				if(cfiCurrentPromotionSection!=null){
					StatementSection PromotionRepeatSection = null;
           			long DetailLines = cfiCurrentPromotionSection.getSectionCount("CurrentPromotionRepeatSection");

            		for (int i = 0; i < DetailLines; i++) {
            		    // Get the row section
            		    PromotionRepeatSection = cfiCurrentPromotionSection.getRepeatSection("CurrentPromotionRepeatSection", i);
  						//Column 3 : Name
	        	        cell = new PdfPCell(new Phrase(PromotionRepeatSection.getField("PromotionLine").asString(), font));
            	    	cell.setHorizontalAlignment(PdfPCell.ALIGN_LEFT);
            	    	cell.setPaddingLeft(tpPadLeft);
            	    	cell.setColspan(2);
            	    	cell.setBorderWidth(0);
            	    	subTable.addCell(cell);
					}//end For
				}
				//Add subTable
				cell = new PdfPCell(subTable);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorder(PdfPCell.BOTTOM);
				table.addCell(cell);

				//Footer
				//Message
				phrase = new Phrase();
				chunk = new Chunk("", font);
				phrase.add(chunk);
				chunk = new Chunk("", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingTop(tpPadTop);
				table.addCell(cell);

				//Payment Slip title
				phrase = new Phrase();
				chunk = new Chunk("PAYMENT SLIP\n", bigFontBold);
				phrase.add(chunk);
				chunk = new Chunk("SLIP BAYARAN", bigFontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
				cell.setBorderWidth(0);
				cell.setPaddingRight(tpPadRight);
				table.addCell(cell);

				//Create Table 2 inside main table to hold 2 sub table
                float[] TableColsWidth2 = {6f, 4f};
                PdfPTable table2 = new PdfPTable(TableColsWidth2);

				//Reuse sub Table inside main table fifth column
                float[] subTableColsWidth3 = {1f, 1f};
                subTable = new PdfPTable(subTableColsWidth3);

                //Account Name title
				phrase = new Phrase();
				chunk = new Chunk("Account Name / ", font);
				phrase.add(chunk);
				chunk = new Chunk("Nama Akaun", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Account Name content
				phrase = new Phrase();
				chunk = new Chunk(": " + AccName, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

                //Account no title
				phrase = new Phrase();
				chunk = new Chunk("Account No. / ", font);
				phrase.add(chunk);
				chunk = new Chunk("No. Akaun", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//Account no content
				phrase = new Phrase();
				chunk = new Chunk(": " + AccNo + " (new) / " + OldAccountNumber, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

                //phone no title
				phrase = new Phrase();
				chunk = new Chunk("Phone Number / ", font);
				phrase.add(chunk);
				chunk = new Chunk("No.Telefon", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//phone no content
				phrase = new Phrase();
				chunk = new Chunk(": " + PhoneNo, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

                //reference number title
				phrase = new Phrase();
				chunk = new Chunk("Reference Number / ", font);
				phrase.add(chunk);
				chunk = new Chunk("No. Rujukan", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//reference number content
				phrase = new Phrase();
				chunk = new Chunk(": " + RefNo, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				subTable.addCell(cell);

				//add big blank row at main table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(2);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				table.addCell(cell);

				//add big blank row at main table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(2);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				table.addCell(cell);

				//Add subTable
				cell = new PdfPCell(subTable);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				table2.addCell(cell);

				//Reuse sub Table inside main table sixth column
                float[] subTableColsWidth4 = {2f, 3f};
                subTable = new PdfPTable(subTableColsWidth4);

				//sub table Total Amount Due header
				phrase = new Phrase();
				chunk = new Chunk("Total Amount Due\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Jumlah Perlu Dibayar ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setGrayFill(0.8f);
				subTable.addCell(cell);

				//sub table header
				phrase = new Phrase();
				chunk = new Chunk("Current Charges Due Date\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk("Tarikh Akhir Bayaran Due Date ", fontItalic);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setGrayFill(0.8f);
				subTable.addCell(cell);

				//sub table Total Amount Due value
				phrase = new Phrase();
				chunk = new Chunk(TotalAmountDue, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//sub table Current charges due date value
				phrase = new Phrase();
				chunk = new Chunk(CurrentChargesDueDate, font);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_CENTER);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				subTable.addCell(cell);

				//Add subTable
				cell = new PdfPCell(subTable);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setPaddingTop(12);
				table2.addCell(cell);

				//Add Table 2
				cell = new PdfPCell(table2);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setBorderWidth(0);
				cell.setColspan(2);
				table.addCell(cell);

				//add big blank row at main table
				cell = new PdfPCell(new Phrase("",fontSpace));
				cell.setBorderWidth(0);
				cell.setColspan(2);
				cell.setPaddingTop(tpPadTop);
				cell.setPaddingBottom(tpPadBottom);
				table.addCell(cell);

				//Account Name and Address
				phrase = new Phrase();
				chunk = new Chunk(AccName + "\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk(Address1 + "\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk(Address2 + "\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk(Address3 + "\n", fontBold);
				phrase.add(chunk);
				chunk = new Chunk(Address4 + "\n", fontBold);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setColspan(2);
				cell.setBorderWidth(0);
				cell.setPaddingLeft(50);
				table.addCell(cell);

				//blank column
                cell = new PdfPCell(new Phrase("", fontBold));
                cell.setBorderWidth(0);
				table.addCell(cell);

				//statement date
				phrase = new Phrase();
				chunk = new Chunk(StmtDt, fontBold);
				phrase.add(chunk);
                cell = new PdfPCell(phrase);
				cell.setHorizontalAlignment(Element.ALIGN_LEFT);
				cell.setPaddingLeft(tpPadLeft);
				cell.setBorderWidth(0);
				table.addCell(cell);

		   	document.add(table);

			document.close();
			}else{ // end if tnc!=""
	            req.setAttribute("downloadUrl", download1stPageUrl);
	            req.setAttribute("fromServlet", "1stPagePDF");

	   // String downloadUrl = (String)request.getAttribute("downloadUrl");
				forwardToMappedUrl(req, response, EbppxConstants.URL_JSP_PARAM);
			}

        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }

	/**
     * format the input String and replace the escape charaters
     * @param str string to format
     * @return formated String or blank String if given null String
     */

    public String formatStr (String str) {

        String strFormat=null;

		if (str == null) {

			return "";

		} else {

            strFormat = str.trim();

			// replace escape character
			strFormat = strFormat.replaceAll("&amp;", "&");
			strFormat = strFormat.replaceAll("&quot;", "\"");
			strFormat = strFormat.replaceAll("&apos;", "'");
			strFormat = strFormat.replaceAll("&lt;", "<");
			strFormat = strFormat.replaceAll("&gt;", ">");

			return strFormat;
		}
	}
}
